# -*- coding: utf-8 -*-
import scrapy
from Blog_Category.items import BlogCategoryItem
# from scrapy.shell import inspect_response


class CsdnSpider(scrapy.Spider):
    name = "csdn"
    allowed_domains = ["csdn.net"]

    def start_requests(self):
        return [scrapy.Request("http://passport.csdn.net/account/login", callback=self.__post_login)]

    @staticmethod
    def __get_key_value(response, keystr):
        # '用来得到关键字段的值'
        # '或者通过css选取器可以如下'
        # return response.css('input[name="lt"] ::attr(value)').extract_first()
        # '或者通过xpath可以如下'
        return response.xpath(u'//input[@name=\"{0}\"]/@value'.format(keystr)).extract_first()

    def __post_login(self, response):
        # '通过post登陆'
        post_data = {
            'username': getattr(self, 'username', None),
            'password': getattr(self, 'password', None),
            'lt': CsdnSpider.__get_key_value(response, 'lt'),
            'execution': CsdnSpider.__get_key_value(response, 'execution'),
            '_eventId': 'submit',
        }
        return [scrapy.FormRequest.from_response(response, formdata=post_data, callback=self.__check_login_status)]

    def __check_login_status(self, response):
        # '用来检测是否登陆成功'
        # 'jspath = response.css('script ::attr(src)').extract_first()'
        jspath = response.xpath('//script/@src').extract_first()
        # 'if succeed '
        if jspath == '/content/loginbox/loginapi.js':
            # 'redirect to the last url'
            return [scrapy.Request("http://write.blog.csdn.net/category", headers=response.headers,
                                   callback=self.parse)]
        else:
            # error_mess = response.xpath('//div[@class="error-mess"]/span[@id="error-message"]/text()').extract_first()
            error_mess = response.css('div[class=error-mess] span[id=error-message]::text').extract_first()
            if error_mess:
                raise scrapy.exceptions.CloseSpider(error_mess)
            else:
                raise scrapy.exceptions.CloseSpider(u'服务器错误 HTTP Error 500: Internal Server Error')

    def parse(self, response):
        # for tdleft in response.xpath('//tbody[@id="lstBody"]/tr'):
        for tdleft in response.css('tbody[id=lstBody] tr'):
            # yield {
            #    # 'category', tdleft.xpath('td[@class="tdleft"]/span/text()').extract_first()
            #    'category': tdleft.css('td[class=tdleft] span::text').extract_first(),
            #    # number': tdleft.xpath('td/a[@class="red"]/text()').extract_first(),
            #    'number': tdleft.css('td a[class=red]::text').extract_first(),
            #}
            yield BlogCategoryItem({
                # 'category', tdleft.xpath('td[@class="tdleft"]/span/text()').extract_first()
                'category': tdleft.css('td[class=tdleft] span::text').extract_first(),
                # number': tdleft.xpath('td/a[@class="red"]/text()').extract_first(),
                'number': tdleft.css('td a[class=red]::text').extract_first(),
            })

